
/*********************************************************************
																			   
	 TITLE: School randomization 
	 PROJECT: Teaching and Learning in Multi-grade Rural Schools: Experimental 
	 Evidence from Colombia											   
																			   
	DESCRIPTION: This dofile randomizes the schools into treatment and control 
	groups.
	It sorts by distance to Bogotá, the cost of the trip, gender and rural/urban.
 
*********************************************************************/

/*********************************************************************
								0. SET UP 		
*********************************************************************/

clear all

*------------------------------------
* 0.1. Set Directories
*------------------------------------

global gen_dir
		
cd "${gen_dir}"
	
*------------------------------------
* 0.2. Set paths
*------------------------------------
	
global data_use 

*------------------------------------
* 0.3. Other settings
*------------------------------------
	
version 13.1
set more off

/*********************************************************************
							1. RANDOMIZE		
*********************************************************************/

*------------------------------------
* 1.1. Call data
*------------------------------------

use "${data_use}/basefinal_sedes_nopii.dta", clear 

count  // 148 schools 

*------------------------------------------------
* 1.2. Randomize
*------------------------------------------------

/* A. Order */

set seed 141650963
sort tmp_bogota_sede costo_trayecto porc_hombre porc_zona
gen bloque_distancia= int((_n-1)/4) +1 

/* B. Generate random numbers */

gen numero_aleatorio=uniform()
	
/* C. Rank numbers */

egen orden_aleatorio=rank(numero_aleatorio),  by (bloque_distancia)

/* D. Assign treatment and control status  */

gen tratamiento=.
replace tratamiento=1 if orden_aleatorio<=2    
replace tratamiento=0 if orden_aleatorio>2  

/* E. Check results */

tab tratamiento

*------------------------------------------------
* 1.3. Save dataset
*------------------------------------------------

save "${data_use}/schools_randomized.dta", replace 
